package top.igotcha.eduservice.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import top.igotcha.eduservice.entity.EduTeacher;
import top.igotcha.eduservice.entity.vo.TeacherQueryVO;
import top.igotcha.eduservice.service.EduTeacherService;
import top.igotcha.commonutils.utils.R;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author Gotcha
 * @since 2021-08-01
 */
@Api(description = "讲师管理")
@RestController
@RequestMapping("/eduservice/teacher")
//@CrossOrigin//解决跨域
public class EduTeacherController {

    @Autowired
    EduTeacherService teacherService;

    @ApiOperation("获取所有讲师")
    @GetMapping("findAll")
    public R findAllTeacher(){
        List<EduTeacher> list = teacherService.list(null);
        return R.OK().data("items",list);
    }

    @ApiOperation("根据id删除讲师")
    @DeleteMapping("{id}")
    public R removeById(
            @ApiParam(name = "id" , value = "讲师id" ,required = true)
            @PathVariable String id){
        boolean flag = teacherService.removeById(id);
        if (flag){
            return R.OK();
        } else {
            return R.ERROR();
        }
    }

    @ApiOperation(value = "分页讲师列表")
    @GetMapping("{page}/{limit}")
    public R pageList(
        @ApiParam(name = "page", value = "当前页码", required = true)
        @PathVariable Long page,

        @ApiParam(name = "limit", value = "每页记录数", required = true)
        @PathVariable Long limit){
        Page<EduTeacher> pageParam = new Page<>(page, limit);
        teacherService.page(pageParam, null);
        List<EduTeacher> records = pageParam.getRecords();
        long total = pageParam.getTotal();
        return  R.OK().data("total", total).data("rows", records);
    }

    @ApiOperation(value = "条件分页讲师列表")
    @PostMapping("{page}/{limit}")
    public R pageTeacherCondition(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,

            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit,

            @ApiParam(name = "teacherQuery", value = "查询对象", required = false)
            @RequestBody(required = false)
            TeacherQueryVO vo){
        Page<EduTeacher> pageParam = new Page<>(page, limit);
        teacherService.pageTeacherCondition(pageParam, vo);
        List<EduTeacher> records = pageParam.getRecords();
        long total = pageParam.getTotal();
        return  R.OK().data("total", total).data("rows", records);
    }


    @ApiOperation(value = "新增讲师")
    @PostMapping()
    public R save(
        @ApiParam(name = "teacher", value = "讲师对象", required = true)
        @RequestBody EduTeacher teacher){
        boolean flag = teacherService.save(teacher);
        if (flag){
            return R.OK();
        } else {
            return R.ERROR();
        }
    }

    @ApiOperation(value = "根据ID查询讲师")
    @GetMapping("{id}")
    public R getById(
    @ApiParam(name = "id", value = "讲师ID", required = true)
    @PathVariable String id){
        EduTeacher teacher = teacherService.getById(id);
        return R.OK().data("item", teacher);
    }

    @ApiOperation(value = "根据ID修改讲师")
    @PutMapping("{id}")
    public R updateById(
    @ApiParam(name = "id", value = "讲师ID", required = true)
    @PathVariable String id,
    @ApiParam(name = "teacher", value = "讲师对象", required = true)
    @RequestBody EduTeacher teacher){
        teacher.setId(id);
        boolean flag = teacherService.updateById(teacher);
        if (flag){
            return R.OK();
        } else {
            return R.ERROR();
        }
    }
}

